package cn.handong.tool.algorithm.DFS;

public class DFStest {

    public int numIslands(char[][] grid){
        int islandNum = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == '1'){
                    dfs(grid,i,j);
                    islandNum++;
                }
            }
        }
        return islandNum;
    }
    public void dfs(char[][] grid,int i,int j){
        if (i < 0 || i >=grid.length || j < 0 || j >= grid[0].length || grid[i][j] != '1'){
            return;
        }
        grid[i][j] = '2';
        dfs(grid,i+1,j);
        dfs(grid,i-1,j);
        dfs(grid,i,j+1);
        dfs(grid,i,j-1);
    }




    // 岛屿数目
    public int numIsLand(char[][] grid){
        int n = grid.length; //行数
        int m = grid[0].length; //列数
        int res = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == '1'){ //入岛
                    dfs1(grid,i,j);
                    res++;
                }
            }
        }
        return res;
    }

    public void dfs1(char[][] grid,int i, int j){
        if (grid[i][j] == '1' && i >= 0 && i < grid.length && j >= 0 && j < grid[0].length){
            grid[i][j] = '2';
            dfs1(grid,i+1,j);
            dfs1(grid,i-1,j);
            dfs1(grid,i,j+1);
            dfs1(grid,i,j-1);
        }
    }




















}
